package com.wiegand.gui.button {
	import com.caurina.transitions.Tweener;
	import com.wiegand.event.FullscreenEvent;
	import com.wiegand.sound.Soundcontroller;
	import com.wiegand.util.ColorRectangle;

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author gandy
	 */
	public class FullscreenButton extends Sprite {
		
		private const _buttonWidth:int=30;
		private const _buttonHeight:int=10;
		
		private const _innerWInactive:Number=_buttonWidth/2;
		private const _innerHInactive:Number=_buttonHeight/2;
		private const _innerXInactive:Number = (_buttonWidth-_innerWInactive)/2;
		private const _innerYInactive:Number= (_buttonHeight-_innerHInactive)/2;
	
		private const _innerWActive:Number=_buttonWidth-4;
		private const _innerHActive:Number=_buttonHeight-4;		
		private const _innerXActive:Number=2;
		private const _innerYActive:Number=2;
		
		private const _outerAlpha:Number=0.5;
		private const _alphaActive:Number=1.0;
		private const _alphaInactive:Number=0.5;
		private const _innerAlphaActive:Number=1.0;
		private const _innerAlphaInactive:Number=0.75;
		
		private var _hit : Sprite;
		private var _state : Boolean = false;
		private var _innerRec : Sprite;
		private var _outerRec : Sprite;

		
		public function FullscreenButton() {
		}
		
		public function init() : void {
			alpha = _alphaInactive;
			
			_outerRec = ColorRectangle.create(0xffffff,_outerAlpha,_buttonWidth,_buttonHeight);
			addChild(_outerRec);
			
			if(!_state){
				_innerRec = ColorRectangle.create(0xffffff,1,_innerWInactive,_innerHInactive);
				_innerRec.x = _innerXInactive;
				_innerRec.y = _innerYInactive;
			}else{
				_innerRec = ColorRectangle.create(0xffffff,1,_innerWActive,_innerHActive);
				_innerRec.x = _innerXActive;
				_innerRec.y = _innerYActive;
			}
			
			_innerRec.alpha = _innerAlphaInactive;
			addChild(_innerRec);
			
			_hit = ColorRectangle.create(0xff0000, 0, _buttonWidth, _buttonHeight);
			_hit.buttonMode = true;
			addChild(_hit);
			
			addListeners();
		}
		
		private function over(e:MouseEvent=null) : void {
			Tweener.removeTweens(this);
			Tweener.addTween(this,{alpha:_alphaActive,time:.5,transition:"linear"});
			Tweener.removeTweens(_innerRec);
			Tweener.addTween(_innerRec,{alpha:_innerAlphaActive,time:.5,transition:"linear"});
			Tweener.addTween(_innerRec,{alpha:_innerAlphaInactive,time:.5,transition:"linear"});
			if(!_state) Tweener.addTween(_innerRec,{x:_innerXActive,y:_innerYActive,width:_innerWActive,height:_innerHActive,time:.5,transition:"easeinexpo"});
			if( _state) Tweener.addTween(_innerRec,{x:_innerXInactive,y:_innerYInactive,width:_innerWInactive,height:_innerHInactive,time:.5,transition:"easeinexpo"});
			Soundcontroller.getInstance().playEffSound("fullscreenBtOver");
		}
		
		private function out(e:MouseEvent=null) : void {
			Tweener.removeTweens(this);
			Tweener.addTween(this,{alpha:_alphaInactive,time:.5,transition:"linear"});
			Tweener.removeTweens(_innerRec);
			Tweener.addTween(_innerRec,{alpha:_innerAlphaInactive,time:.5,transition:"linear"});
			if(!_state) Tweener.addTween(_innerRec,{x:_innerXInactive,y:_innerYInactive,width:_innerWInactive,height:_innerHInactive,time:.5,transition:"easeinexpo"});
			if( _state) Tweener.addTween(_innerRec,{x:_innerXActive,y:_innerYActive,width:_innerWActive,height:_innerHActive,time:.5,transition:"easeinexpo"});
		}

		private function click(e:MouseEvent) : void {			
			if(!_state){
				_innerRec.width = _innerWInactive;
				_innerRec.height = _innerHInactive;
				_innerRec.x = _innerXInactive;
				_innerRec.y = _innerYInactive;
				Tweener.removeTweens(_innerRec);
				Tweener.addTween(_innerRec,{x:_innerXActive,y:_innerYActive,width:_innerWActive,height:_innerHActive,time:.5,transition:"easeoutexpo"});
			}else{
				_innerRec.width = _innerWActive;
				_innerRec.height = _innerHActive;
				_innerRec.x = _innerXActive;
				_innerRec.y = _innerYActive;
				Tweener.removeTweens(_innerRec);
				Tweener.addTween(_innerRec,{x:_innerXInactive,y:_innerYInactive,width:_innerWInactive,height:_innerHInactive,time:.5,transition:"easeoutexpo"});
			}
			_state=!_state;
			dispatchEvent( new FullscreenEvent( FullscreenEvent.CHANGE, _state ) );
		}
		
		private function addListeners():void{
			_hit.addEventListener(MouseEvent.CLICK, click);
			_hit.addEventListener(MouseEvent.MOUSE_OVER, over);
			_hit.addEventListener(MouseEvent.MOUSE_OUT, out);		
		}
		
		private function removeListeners():void{
			_hit.removeEventListener(MouseEvent.CLICK, click);
			_hit.removeEventListener(MouseEvent.MOUSE_OVER, over);
			_hit.removeEventListener(MouseEvent.MOUSE_OUT, out);	
		}
		
		public function lock():void{
			removeListeners();
		}
		
		public function get state():Boolean{
			return _state;
		}
		
		public function unlock():void{
			addListeners();
		}
		
	}
}
